package com.caozhi.mspeoject.nb_distributed_id.service;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;


@Service
public class CreatedDistributeIdImpl implements CreatedDistributeId {

    public Long get() {
        Snowflake snowflake = IdUtil.getSnowflake(2, 30);
        return snowflake.nextId();
    }
    public Long MacAddressFetcher() {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetAddress);
            if (networkInterface != null) {
                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null && mac.length == 6) { // Ensure it's a valid MAC address length
                    long macAsLong = bytesToLong(mac);
                    System.out.println("Local MAC Address as Long: " + macAsLong);
                    // If you also want to print the MAC address in hex format:
                    System.out.println("Local MAC Address: " + String.format("%012x", macAsLong));
                    return macAsLong;
                } else {
                    System.out.println("Could not get hardware address.");
                }
            } else {
                System.out.println("Could not find network interface.");
            }
        } catch (SocketException | java.net.UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static long bytesToLong(byte[] bytes) {
        long result = 0;
        for (byte b : bytes) {
            result = (result << 8) | (b & 0xFF);
        }
        return result;
    }

}
